<?php

namespace Client\Repository;

use Doctrine\ORM\EntityRepository;

class ClientRepository extends EntityRepository {

    /**
     * 
     * @param \Client\Entity\ClientCriteria $criteria
     */
    public function findByCriteria($criteria) {
        $empty = true;
       
        $qb = $this->createQueryBuilder('c');
        
        $where = $qb->expr()->andX();
        
        if (!empty($criteria->getFirstName())) {
            $where->add($qb->expr()->like('c.firstName', ':firstName'));
            $qb->setParameter('firstName', $criteria->getFirstName());
            $empty = false;
        }
        if (!empty($criteria->getLastName())) {
            $where->add($qb->expr()->like('c.lastName', ':lastName'));
            $qb->setParameter('lastName', $criteria->getLastName());
            $empty = false;
        }
        if (!empty($criteria->getAdres())) {
            $where->add($qb->expr()->like('c.adres', ':adres'));
            $qb->setParameter('adres', $criteria->getAdres());
            $empty = false;
        }
        if (!empty($criteria->getPhone())) {
            $where->add($qb->expr()->like('c.phone', ':phone'));
            $qb->setParameter('phone', $criteria->getPhone());
            $empty = false;
        }
        
        if($empty) {
            return array();
        }
        
        return $qb->where($where)
                ->getQuery()
                ->getResult();
    }

}
